import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:qgchat/common/global/global.dart';

/// 项目中日常使用的提示框，如：余额不足等
class TipsDialog {
  /// 测试
  static test(BuildContext context, List<String> msg) async {
    await showDialog(
      context: context,
      builder: (BuildContext context) {
        return dialogBuild(
          context,
          title: "测试",
          msg: msg,
        );
      },
    );
  }

  /// 引导登录
  static login(BuildContext context, Function callback) async {
    await showDialog(
      context: context,
      builder: (BuildContext context) {
        return dialogBuild(
          context,
          title: "登录后解锁更多功能喔~",
          confirmText: "现在就登录",
          confirmFn: callback,
        );
      },
    );
  }

  /// 关闭app
  static tryCloseApp(BuildContext context, Function callback) async {
    await showDialog(
      context: context,
      builder: (BuildContext context) {
        return dialogBuild(
          context,
          title: "不留恋多一会吗？",
          msg: ["确定退出吗"],
          confirmFn: callback,
        );
      },
    );
  }

  /// 关闭视频
  static tryCloseVideo(BuildContext context, Function callback) async {
    await showDialog(
      context: context,
      builder: (BuildContext context) {
        return dialogBuild(
          context,
          title: "不留恋多一会吗？",
          msg: ["确定结束视频通话"],
          confirmFn: callback,
        );
      },
    );
  }

  /// 免费试看提示
  TipsDialog.tryLook(BuildContext context, Function callback) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return dialogBuild(
          context,
          msg: ["本次通话将扣取1次免费试看机会是否继续操作"],
          confirmText: "继续",
          confirmFn: callback,
        );
      },
    );
  }

  /// 尝试进行视频时，收费弹窗
  TipsDialog.tryVideo(BuildContext context, gold, Function callback) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return dialogBuild(
          context,
          msg: ["本次通话将以 $gold 金币/分钟扣取是否继续操作"],
          confirmText: "继续",
          confirmFn: callback,
        );
      },
    );
  }

  /// 尝试进行视频时提示
  TipsDialog.tryToVideo(BuildContext context, String name, Function callback) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return dialogBuild(
          context,
          msg: ["是否与 $name 视频通话"],
          confirmText: "继续",
          confirmFn: callback,
        );
      },
    );
  }

  /// 尝试进行回拨时提示
  TipsDialog.tryToCallback(BuildContext context, String name, Function callback) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return dialogBuild(
          context,
          msg: ["是否回拨 $name"],
          confirmText: "继续",
          confirmFn: callback,
        );
      },
    );
  }

  /// 试看结束
  TipsDialog.tryLookEnd(BuildContext context, gold, Function callback) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return dialogBuild(
          context,
          msg: [
            "试看即将在30秒后结束，继续观看则将以 $gold 金币/分钟扣取",
            "是否继续操作",
          ],
          confirmText: "继续",
          confirmFn: callback,
        );
      },
    );
  }

  /// 余额不足
  TipsDialog.notGold(BuildContext context, Function callback) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return dialogBuild(
          context,
          msg: ["您的金币余额不足与主播通话1分钟！是否立即充值？"],
          confirmText: "去充值",
          confirmFn: callback,
        );
      },
    );
  }

  /// 余额不足,视频即将结束
  TipsDialog.notGoldReadyToEnd(BuildContext context, gold, Function callback) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return dialogBuild(
          context,
          msg: ["观众金币余额不足与主播通话1分钟视频即将在10秒后结束"],
          confirmText: "好的",
          confirmFn: callback,
        );
      },
    );
  }

  /// 预约主播
  TipsDialog.makeAnchor(BuildContext context, Function callback) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return dialogBuild(
          context,
          title: "预约主播",
          msg: [
            "主播暂时无法接通",
            "请稍后再拨,或者立刻预约Ta",
            "账户香币数量大于0才能预约主播哦",
            "预约后平台将立即短信通知主播",
          ],
          confirmText: "预约Ta",
          confirmFn: callback,
        );
      },
    );
  }

  /// 账号在别处登录
  TipsDialog.accountLoginElsewhere(BuildContext context, Function callback) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return dialogBuild(
          context,
          title: "警告",
          msg: [
            "账号在别处登录",
          ],
          closeFn: callback,
          confirmFn: callback,
        );
      },
    );
  }

  /// 余额不足送礼物
  TipsDialog.noGoldGivingGifts(BuildContext context, Function callback) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return dialogBuild(
          context,
          title: "提示",
          msg: [
            "您的余额不足以赠送礼物哟",
            "是否前去充值",
          ],
          confirmText: "去充值",
          confirmFn: callback,
        );
      },
    );
  }

  /// 应付华为的假举报
  TipsDialog.report(BuildContext context, callback) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return dialogBuild(
          context,
          title: "是否举报该用户",
          confirmFn: callback,
        );
      },
    );
  }

}

// 弹出窗构造函数
dialogBuild(
  BuildContext context, {
  String title = '提示',
  List<String> msg = const [],
  String closeText = '取消',
  String confirmText = '确认',
  Function closeFn,
  Function confirmFn,
}) {
  final Widget titleBuild = Text(
    "$title",
    style: TextStyle(
      color: col("1e1e1e"),
      fontSize: px(34),
      fontWeight: FontWeight.bold,
    ),
  );

  final List<Widget> contentsBuild = msg.map<Widget>((item) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: px(10)),
      child: Text(
        "$item",
        style: TextStyle(
          color: col("1e1e1e"),
          fontSize: px(26),
        ),
      ),
    );
  }).toList();

  return CupertinoAlertDialog(
    title: titleBuild,
    content: SingleChildScrollView(
      child: ListBody(
        children: contentsBuild,
      ),
    ),
    actions: <Widget>[
      CupertinoDialogAction(
        child: Text(
          "$closeText",
          style: TextStyle(
            color: col("909399"),
            fontSize: px(34),
          ),
        ),
        onPressed: () {
          Navigator.pop(context);
          if (closeFn != null) {
            closeFn();
          }
        },
      ),
      CupertinoDialogAction(
        child: Text(
          "$confirmText",
          style: TextStyle(
            color: col("5640c6"),
            fontSize: px(34),
            fontWeight: FontWeight.bold,
          ),
        ),
        onPressed: () {
          Navigator.pop(context);
          if (confirmFn != null) {
            confirmFn();
          }
        },
      ),
    ],
  );
}
